package eScrum;

import java.io.Serializable;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.spring.injection.annot.SpringBean;

import eScrum.entities.ClassicUser;
import eScrum.entities.Developer;
import eScrum.entities.UserRole;
import eScrum.entities.UserStatus;
import eScrum.pages.AdminDashboard;
import eScrum.pages.BasePage;
import eScrum.pages.MainDashboard;
import eScrum.services.UserService;

public class HomePage extends WebPage {
	private static final long serialVersionUID = 1L;

	@SpringBean
	private UserService userService;

	private UserSession userSession = (UserSession) getSession();
	private UserModel model = new UserModel();

	public HomePage(final PageParameters parameters) {
		super(parameters);
		CompoundPropertyModel<UserModel> userModel = new CompoundPropertyModel<UserModel>(model);
		Form<UserModel> loginForm = new Form<UserModel>("loginform", userModel);
		final FeedbackPanel feedbackPanel = new FeedbackPanel("feedbackPanel");
		final TextField<String> usernameTextField = new RequiredTextField<String>("username", userModel.<String> bind("username"));
		TextField<String> passwordTextField = new PasswordTextField("password", userModel.<String> bind("password"));
		loginForm.add(usernameTextField);
		loginForm.add(passwordTextField);
		loginForm.add(feedbackPanel);
		loginForm.setOutputMarkupId(true);
		feedbackPanel.setOutputMarkupId(true);

		loginForm.add(new AjaxButton("login", loginForm) {

			private static final long serialVersionUID = 7570142608871427683L;

			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				try {
					target.add(feedbackPanel);
					authenticateUser();
				} catch (Exception e) {

				}
			}

			@Override
			protected void onError(AjaxRequestTarget target, Form<?> form) {
				target.add(feedbackPanel);
			}
		});
		add(loginForm);
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	private void authenticateUser() {
		if (userSession.alreadySignedIn()) {
			userSession.setUser(null);
		}
		if (userSession.signIn(model.getUsername(), model.getPassword())) {
			UserStatus status = userSession.getUser().getStatus();
			if (status == UserStatus.ACTIVE) {
				String role = userSession.getRoles().toString();
				if (role.equals(UserRole.ADMIN.toString()))
					setResponsePage(AdminDashboard.class);
				else {
					setResponsePage(MainDashboard.class);
				}
			} else if (status == UserStatus.SUSPENDED)
				info(getString("accountSuspended"));
		} else
			error(getString("incorrectCredentials"));
	}

	class UserModel implements Serializable {

		private static final long serialVersionUID = -8006760335786093385L;

		private long id;
		private String firstName;
		private String lastName;
		private String email;

		private String username;
		private String password;
		private UserStatus status;
		private UserRole role;

		private Developer developer;
		private ClassicUser classicUser;

		public long getId() {
			return id;
		}

		public void setId(long id) {
			this.id = id;
		}

		public String getFirstName() {
			return firstName;
		}

		public void setFirstName(String firstName) {
			this.firstName = firstName;
		}

		public String getLastName() {
			return lastName;
		}

		public void setLastName(String lastName) {
			this.lastName = lastName;
		}

		public String getEmail() {
			return email;
		}

		public void setEmail(String email) {
			this.email = email;
		}

		public String getUsername() {
			return username;
		}

		public void setUsername(String username) {
			this.username = username;
		}

		public String getPassword() {
			return password;
		}

		public void setPassword(String password) {
			this.password = password;
		}

		public UserStatus getStatus() {
			return status;
		}

		public void setStatus(UserStatus status) {
			this.status = status;
		}

		public UserRole getRole() {
			return role;
		}

		public void setRole(UserRole role) {
			this.role = role;
		}

		public Developer getDeveloper() {
			return developer;
		}

		public void setDeveloper(Developer developer) {
			this.developer = developer;
		}

		public ClassicUser getClassicUser() {
			return classicUser;
		}

		public void setClassicUser(ClassicUser classicUser) {
			this.classicUser = classicUser;
		}
	}

}
